Java 获取resources下的文件路径和创建临时文件

       之前处理根据模板文件,批量导入xxx.zip 的下载功能,用到这两个知识,就简单记录下,对于流的处理就跳过了      

由于maven项目打包会把 src/main/java 和 src/main/resources 下的文件放到 target/classes 下,所以统一以根路径代表此目录。

创建一个springboot项目

      

server:
  port: 80
  servlet:
    context-path: /JQ_Resource

 

一、获取resources下的文件路径

总结起来有两点:

1、Class.getResource()的获取资源路径

      - 如果以 / 开头,则从根路径开始搜索资源。

      - 如果不以 / 开头,则从当前类所在的路径开始搜索资源。

2、ClassLoader.getResource()的资源获取不能以 / 开头,统一从根路径开始搜索资源。

String path = this.getClass().getClassLoader().getResource("xxx").getPath();

测试:

    public void getResource() {
        //1、通过Class的getResource方法
        String a1 = RescourceController.class.getResource("/cn/jq/jqresource/pojo/User.class").getPath();
        String a2 = this.getClass().getResource("../pojo/User.class").getPath();
        String a3 = RescourceController.class.getResource("/static/a.txt").getPath();
        String a4 = this.getClass().getResource("../../../../static/a.txt").getPath();
        System.out.println(a1.equals(a2)); // true
        System.out.println(a4); // /D:/JQ/workspace/JQ_Resource/target/classes/static/a.txt

        // 2、通过本类的ClassLoader的getResource方法
        String b1 = RescourceController.class.getClassLoader().getResource("cn/jq/jqresource/pojo/User.class").getPath();
        String b2 = this.getClass().getClassLoader().getResource("static/a.txt").getPath();
        String b3 = this.getClass().getClassLoader().getResource("static/resource/jq.docx").getPath();

        // 3、通过ClassLoader的getSystemResource方法
        String c1 = ClassLoader.getSystemClassLoader().getResource("cn/jq/jqresource/pojo/User.class").getPath();
        String c2 = ClassLoader.getSystemClassLoader().getResource("static/a.txt").getPath();
        String c3 = ClassLoader.getSystemClassLoader().getResource("static/resource/jq.docx").getPath();

        // 4、通过ClassLoader的getSystemResource方法
        String d1 = ClassLoader.getSystemResource("cn/jq/jqresource/pojo/User.class").getPath();
        String d2 = ClassLoader.getSystemResource("static/a.txt").getPath();
        String d3 = ClassLoader.getSystemResource("static/resource/jq.docx").getPath();

        // 5、通过Thread方式的ClassLoader的getResource方法
        String e1 = Thread.currentThread().getContextClassLoader().getResource("cn/jq/jqresource/pojo/User.class").getPath();
        String e2 = Thread.currentThread().getContextClassLoader().getResource("static/a.txt").getPath();
        String e3 = Thread.currentThread().getContextClassLoader().getResource("static/resource/jq.docx").getPath();
    }

二、resources下创建临时文件

    public void createFile(HttpServletRequest request) throws IOException {
        String contextPath = request.getContextPath(); // /JQ_Resource

        String filePath = contextPath + "/temp/hr.zip";
        String dirPath = contextPath + "/temp/hr";
        File file = new File(filePath);
        File dir = new File(dirPath);
        if (file.exists()) {
            // 删除指定文件,不存在报异常
            FileUtils.forceDelete(file);
        }
        file.createNewFile();


        if (dir.isDirectory()) {
            // 清除该目录下的文件及子目录文件而不删除该目录文件夹。该目录不存在会报错
            FileUtils.cleanDirectory(dir);
        } else {
            dir.mkdirs();
        }

        File dir_File = new File(dirPath + "/" + "dir_file.txt");

        System.out.println(dir_File.getPath()); // \JQ_Resource\temp\hr\dir_file.txt
        System.out.println(file.exists()); // true
    }

知识点回顾:

    Servlet的继承体系与HttpServletRequset和HttpServletResponse

    FileUtils工具类常用方法

  

ends~

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值